﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.WpfShell.ServiceRef;
using SmartSpace.WpfShell.Modules.Dynamic.Util;
using SmartSpace.WpfShell.Modules.Dynamic.Windows;

namespace SmartSpace.WpfShell.Modules.Dynamic
{
    /// <summary>
    /// Interaction logic for CreateTemplateView.xaml
    /// </summary>
    public partial class CreateTemplateView : UserControl, ISsView
    {
        private Guid _workflowId { get; set; }
        private Guid _activityId { get; set; }
        /// <summary>
        /// 详细的数据对象
        /// </summary>
        private DetailData _detailData { get; set; }

        public CreateTemplateView()
        {
            InitializeComponent();

            // 隐藏流程流转面板
            _ctlPanelEvent.Visibility = System.Windows.Visibility.Collapsed;

            // 显示创建按钮
            _ctlBtnCreate.Visibility = System.Windows.Visibility.Visible;

            // 隐藏其他按钮
            _ctlBtnSave.Visibility = System.Windows.Visibility.Collapsed;
        }

        public void LoadArguments(Dictionary<string, string> args)
        {
            _workflowId = new Guid(args["workflowid"]);
            _activityId = new Guid(args["activityid"]);

            ServiceRef.SsWebService client = App.GetProxy();
            client.GetDetailMetadataCompleted += new GetDetailMetadataCompletedEventHandler(client_GetDetailMetadataCompleted);
            client.GetDetailMetadataAsync(_workflowId, _activityId);
        }

        void client_GetDetailMetadataCompleted(object sender, GetDetailMetadataCompletedEventArgs e)
        {
            if (e.Error==null && e.Result.IsSuccess)
            {
                foreach (FieldMetadata item in e.Result.Data.Fields)
                {
                    TextBlock label = new TextBlock();
                    label.Margin = new Thickness(5);
                    label.FontSize = 14;
                    label.Text = item.Caption;
                    _ctlContainer.Children.Add(label);

                    FrameworkElement input = UIBuilder.CreateFieldControl(item);
                    _ctlContainer.Children.Add(input);

                }

                ServiceRef.SsWebService client = App.GetProxy();
                client.GetEmptyDataCompleted += new GetEmptyDataCompletedEventHandler(client_GetEmptyDataCompleted);
                client.GetEmptyDataAsync(_workflowId, _activityId);
            }
        }

        void client_GetEmptyDataCompleted(object sender, GetEmptyDataCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _detailData = e.Result.Data;
                this.DataContext = e.Result.Data;
            }
        }

        #region 创建数据
        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            ServiceRef.SsWebService client = App.GetProxy();
            client.CreateDataCompleted += new CreateDataCompletedEventHandler(client_CreateDataCompleted);
            client.CreateDataAsync(this.DataContext as DetailData);
        }

        void client_CreateDataCompleted(object sender, CreateDataCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                this.DataContext = e.Result.Data;

                // 重新加载界面
                _ctlPanelEvent.Visibility = System.Windows.Visibility.Visible;
                // 隐藏创建按钮
                _ctlBtnCreate.Visibility = System.Windows.Visibility.Collapsed;
                // 显示其他按钮
                _ctlBtnSave.Visibility = System.Windows.Visibility.Visible;

                MessageBox.Show("保存成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else if (e.Error == null)
            {
                MessageBox.Show(e.Result.Message, "保存失败", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else
            {
                MessageBox.Show(e.Error.Message, "保存失败", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion

        #region 保存
        private void _ctlBtnSave_Click(object sender, RoutedEventArgs e)
        {

        }
        #endregion

        #region 提交流程
        private void _ctlBtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            EventMetadata selectedEvent = _ctlListEvent.SelectedItem as EventMetadata;
            ServiceRef.SsWebService client = App.GetProxy();
            client.GetUsersCompleted += new GetUsersCompletedEventHandler(client_GetUsersCompleted);
            client.GetUsersAsync(_workflowId, selectedEvent.Id);
        }

        void client_GetUsersCompleted(object sender, GetUsersCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                SelectToObjectWindow window = new SelectToObjectWindow(e.Result.Data);
                if (window.ShowDialog() == true)
                {
                    EventMetadata selectedEvent = _ctlListEvent.SelectedItem as EventMetadata;
                    ServiceRef.SsWebService client = App.GetProxy();
                    client.DriveCompleted += new DriveCompletedEventHandler(client_DriveCompleted);
                    client.DriveAsync(_workflowId, _activityId, selectedEvent.Id, this.DataContext as DetailData, window.SelectedUser);
                }
            }
        }

        void client_DriveCompleted(object sender, DriveCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                // 提交成功
                MessageBox.Show("提交成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("提交失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion
    }
}
